package com.estudio.moviequizk;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TCPClient {

	private String serverMessage;
	public static final String SERVERIP = "95.108.57.114"; // your computer IP
															// address
	public static final int SERVERPORT = 40001;

	private DataOutputStream dos;
	private DataInputStream dis;
	private Bitmap bitmap;

	public Bitmap getImage(int image_number) {

		try {
			// connecting to server
			InetAddress serverAddr = InetAddress.getByName(SERVERIP);
			Socket socket = new Socket(serverAddr, SERVERPORT);

			try {

				dos = new DataOutputStream(socket.getOutputStream());
				dis = new DataInputStream(socket.getInputStream());

				// sending message to server which image we want to get
				dos.writeUTF("IMG" + Integer.toString(image_number));

				// taking coded message
				serverMessage = dis.readUTF();

				// disconnect
				dis.close();
				dos.close();
				socket.close();

				// decoding message
				byte[] decodedMessage;
				decodedMessage = Base64.decode(serverMessage);

				// converting message to bitmap
				bitmap = BitmapFactory.decodeByteArray(decodedMessage, 0,
						decodedMessage.length);

				return bitmap;

			} catch (Exception e) {

				Log.e("TCP", "S: Error", e);
				return null;

			}

		} catch (Exception e) {

			Log.e("TCP", "C: Error", e);
			return null;

		}

	}

	public String getAnswer(int image_number) {

		try {
			// connecting to server
			InetAddress serverAddr = InetAddress.getByName(SERVERIP);
			Socket socket = new Socket(serverAddr, SERVERPORT);

			 try {

				dos = new DataOutputStream(socket.getOutputStream());
				dis = new DataInputStream(socket.getInputStream());

				// sending message to server which answer we want to get
				dos.writeUTF("ANS" + Integer.toString(image_number));

				// taking answer
				serverMessage = dis.readUTF();

				// disconnect
				dis.close();
				dos.close();
				socket.close();

				return serverMessage;

			} catch (Exception e) {

				Log.e("TCP", "S: Error", e);
				return null;

			}

		} catch (Exception e) {

			Log.e("TCP", "C: Error", e);
			return null;

		}
	}
		public int getCount() {

			try {
				// connecting to server
				InetAddress serverAddr = InetAddress.getByName(SERVERIP);
				Socket socket = new Socket(serverAddr, SERVERPORT);

				 try {

					dos = new DataOutputStream(socket.getOutputStream());
					dis = new DataInputStream(socket.getInputStream());

					// sending message to server which answer we want to get
					dos.writeUTF("NUM");

					// taking answer
					serverMessage = dis.readUTF();

					// disconnect
					dis.close();
					dos.close();
					socket.close();

					return Integer.parseInt(serverMessage);

				} catch (Exception e) {

					Log.e("TCP", "S: Error", e);
					return 0;

				}

			} catch (Exception e) {

				Log.e("TCP", "C: Error", e);
				return 0;

			}

	}

}
